import React from "react";
import {inject, observer} from "mobx-react";
import {Route, withRouter} from "react-router-dom";
import {toJS} from "mobx";
import {ListViewPage, RouteLayout} from "weaver-mobile-page";
import {Icon, Modal, SearchBar, Tools, Bubble} from "weaver-mobile";
import Condition from "./quoteCondition";

const getLabel = Tools.getLabel;

const programStatus = {
    "0": getLabel(220, "草稿"),
    "1": getLabel(19134, "审批中"),
    "2": getLabel(236, "退回"),
    "3": getLabel(508866, "已通过"),
};

/**
 * 方案引用或指标引用
 * jiansong 2019/11/8
 */
@inject("programViewStore")
@withRouter
@observer
export default class Quote extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            path: this.props.location.pathname
        }
        this.store = this.props.programViewStore;
    }

    componentDidMount() {
        this.store.handleQuoteParamsChange({current: 1});
        this.store.buildQuoteData(false);
    }

    render() {
        const {programQuoteStore, quoteParams, buildQuoteData, handleQuoteParamsChange} = this.props.programViewStore;
        return (
            <div className="workrelate-performance-programView-quote workrelate-listCard-reset">
                <SearchBar
                    value={quoteParams.programname}
                    placeholder={getLabel(500351, "请输入关键字")}
                    showExtraButton
                    extraButton={this.buildExtraButton()}
                    onChange={val => handleQuoteParamsChange({"programname": val})}
                    onSubmit={() => buildQuoteData(false)}
                />
                <ListViewPage.Normal
                    showCheck
                    heightSpace={window.getThemeVars ? window.getThemeVars()["hd"] * 49 : 52}
                    multipleCheck={false}
                    getDataCustom={true}
                    store={programQuoteStore}
                    getTemplate={this.buildTemplate}
                    onRefresh={this.handleRefresh}
                    onEndReached={this.handleEndReached}
                    onRowClick={data => this.handleRowClick(data)}
                />
                <RouteLayout.ButtonGroup data={[{
                    key: "cancel",
                    text: getLabel(31129, "取消"),
                    onClick: () => this.props.history.go(-1),
                }, {
                    key: "sure",
                    type: "primary",
                    text: getLabel(826, "确定"),
                    onClick: () => this.handleQuote(),
                }]}/>
                <Route
                    key="quote-condition"
                    path={this.state.path + "/condition"}
                    breadcrumbName="引用方案-条件"
                    render={() => (
                        <RouteLayout>
                            <Condition/>
                        </RouteLayout>
                    )}
                />
            </div>
        );
    }

    //获取搜索框额外按钮
    buildExtraButton = () => {
        return [{
            key: "filter",
            show: <Icon
                type="filter-o"
                onClick={() => this.props.history.push(this.state.path + "/condition")}/>
        }]
    }

    //构建列表样式
    buildTemplate = (template) => {
        return {
            default: [{
                key: "col_1",
                render: (data) => [
                    data.programname,
                    <div className="programView-quote-content-other">
                        <span>{programStatus[data.status]}</span>
                        <span>{data.startdate}</span>
                    </div>
                ]
            }]
        }
    }

    //引用指标方案
    handleQuote = () => {
        const {programQuoteStore: {selectedRowKeys}} = this.store;
        let {quoteProgram, handleProgramQuote} = this.store;
        if (selectedRowKeys.length == 0) {
            Bubble.warning(getLabel(501159, "请选择要操作的数据"))
            return
        }
        Modal.alert(getLabel(386786, "信息确认"), quoteProgram ? getLabel(513688, "确定引用该方案?") : getLabel(513689, "确定引用该方案的考核指标?"), [
            {text: getLabel(31129, "取消")},
            {
                text: getLabel(826, "确定"), onPress: () => {
                    handleProgramQuote(quoteProgram ? "quote" : "quoteindex", selectedRowKeys[0], this.props.updateListData);
                    this.props.history.go(-1);
                }
            }
        ])
    }

    //下拉刷新
    handleRefresh = () => {
        const {programQuoteStore, handleQuoteParamsChange, buildQuoteData} = this.store;
        handleQuoteParamsChange({current: 1});
        buildQuoteData(false);
    }

    //底部加载
    handleEndReached = () => {
        const {handleQuoteParamsChange, quoteParams, buildQuoteData} = this.store;
        handleQuoteParamsChange({current: quoteParams.current + 1});
        buildQuoteData(true);
    }

    //行点击事件
    handleRowClick = (data) => {
        const {programQuoteStore} = this.store;
        if (data.id == programQuoteStore.selectedRowKeys[0]) {
            programQuoteStore.select([])
        } else {
            programQuoteStore.select([data.id])
        }
    }

}


